<template>
  <abstract-dialog
    :loading="loading"
    append-to-body
    :title="title"
    :value="value"
    @close="cancel"
    @open="open"
  >
    <abstract-form ref="form" :model="form" :rules="rules">
      <el-form-item label="人事编号" prop="number">
        <el-input v-model="form.number" placeholder="请输入人事编号"></el-input>
      </el-form-item>
      <el-form-item label="人群标记" prop="crowdMarkerIds">
        <el-select
          ref="elSelect"
          v-model="form.crowdMarkerIds"
          filterable
          multiple
          collapse-tags
          placeholder="请选择人群标记"
        >
          <el-option
            v-for="(i, key) in crowdTags"
            :key="key"
            :label="i.title"
            :value="i.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="tel">
        <el-input v-model="form.tel" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
    </abstract-form>
    <template v-slot:footer>
      <el-button plain size="small" @click="closeDialog">取 消</el-button>
      <el-button size="small" type="primary" @click="confirm">确 定</el-button>
    </template>
  </abstract-dialog>
</template>

<script>
import AbstractDialog from '@/components/abstract/Dialog'
import dialogMixin from '@/mixin/dialogMixin'
import AbstractForm from '@/components/abstract/Form'
import { elSuccess } from '@/utils/message'
import { request } from '@/api/request'

export default {
  name: 'AddDialog',

  components: {
    AbstractDialog,
    AbstractForm
  },

  mixins: [dialogMixin],

  props: {
    value: Boolean,
    data: { type: Object, default: () => ({}) }
  },

  data() {
    // let validateAginPassword = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请再次输入密码'))
    //   } else if (value !== this.form.password) {
    //     callback(new Error('两次输入密码不一致!'))
    //   } else {
    //     callback()
    //   }
    // }

    return {
      users_id: '',
      loading: false,
      crowdTags: [],
      title: '添加用户',
      form: {
        number: null,
        crowdMarkerIds: [],
        name: null,
        tel: null,
        email: null,
        password: 123456,
        aginPassword: 123456
      },
      rules: {
        number: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        tel: [{ required: true, message: '请输入手机号码', trigger: 'blur' }]
        // password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        // aginPassword: [
        //   { required: true, message: '请输入确认密码', trigger: 'blur' },
        //   { validator: validateAginPassword, trigger: 'blur' }
        // ]
      }
    }
  },

  methods: {
    open() {
      this.loading = true
      request('company/users/allUsers/add', {}, (res) => {
        this.crowdTags = res.data
      }).finally(() => (this.loading = false))
    },

    clearForm() {
      this.form.number = null
      this.form.crowdMarkerIds = []
      this.form.name = null
      this.form.tel = null
      this.form.email = null
      this.form.password = 123456
      this.form.aginPassword = 123456
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    cancel() {
      this.closeDialog()
      this.clearForm()
    },

    confirm() {
      if (this.loading) return
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        const promise = request(
          'company/users/allUsers/addSubmit',
          {
            ...this.form,
            crowdMarkerIds: JSON.stringify(this.form.crowdMarkerIds)
          },
          () => {},
          { isDelNull: true }
        )
        promise
          .then(() => {
            elSuccess()
            this.$emit('success')
            this.cancel()
          })
          .finally(() => this.$nextTick(() => (this.loading = false)))
      })
    }
  }
}
</script>
